/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package businesscard.DAO;

import bc.entity.Bophan;
import bc.entity.Capbac;
import bc.entity.Congty;
import bc.entity.Hoadon;
import bc.entity.Lienhe;
import bc.entity.Nganh;
import bc.entity.Quanhuyen;
import bc.entity.Thanhpho;
import bc.entity.User;
import bc.util.HibernateUtil;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author Welcome
 */
public class LienheDAO {

    private Session session;

    public LienheDAO() {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        // this.session = factory.getCurrentSession();
        this.session = factory.openSession();
    }

    public void Save(Lienhe A) {
        try {
            session.getTransaction().begin();
            session.save(A);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }

    public Lienhe[] getAllLienHe() {
        try {
            session.getTransaction().begin();
            String sql = "from Lienhe ORDER BY idLienhe DESC LIMIT 0,5";
            Query query = session.createQuery(sql);
            List result = query.list();
            Lienhe[] lienheList = new Lienhe[result.size()];
            result.toArray(lienheList);
            session.flush();
            session.getTransaction().commit();
            return lienheList;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public Lienhe[] getLienHeTheoTrang(int id_user, int start, int perpage) {
        try {
            //session.getTransaction().begin();
            String sql = "from Lienhe where id_user = ? ORDER BY idLienhe DESC";
            Query query = session.createQuery(sql);
            query.setInteger(0, id_user);
            query.setFirstResult(start);
            query.setMaxResults(perpage);
            List result = query.list();
            Lienhe[] lienheList = new Lienhe[result.size()];
            result.toArray(lienheList);
//            session.flush();
//            session.getTransaction().commit();
            return lienheList;
        } catch (Exception e) {
//            if (session.getTransaction().isActive()) {
//                session.getTransaction().rollback();
//            }
            e.printStackTrace();
        }
        return null;
    }

    public Lienhe getLienHe1(int id_lienhe) {
        try {
            session.getTransaction().begin();
            String sql = "from Lienhe where id_lienhe = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, id_lienhe);
            Lienhe result = (Lienhe) query.uniqueResult();
            session.flush();
            session.getTransaction().commit();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public Lienhe[] getLienHe(int id_user) {
        try {
            // session.getTransaction().begin();
            String sql = "from Lienhe where id_user=? ORDER BY id_lienhe DESC";
            Query query = session.createQuery(sql);
            query.setInteger(0, id_user);
            List result = query.list();
            Lienhe[] lienheList = new Lienhe[result.size()];
            result.toArray(lienheList);
            //session.flush();
            // session.getTransaction().commit();
            return lienheList;
        } catch (Exception e) {
//            if (session.getTransaction().isActive()) {
//                session.getTransaction().rollback();
//            }
            e.printStackTrace();
        }
        return null;
    }

    public String getTenCongty(int id_congty) {
        try {
            // session.getTransaction().begin();
            String sql = "from Congty where id_congty = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, id_congty);
            Congty result = (Congty) query.uniqueResult();
            String congty = result.getCongty();
           // session.flush();
            // session.getTransaction().commit();
            return congty;
        } catch (Exception e) {
//            if(session.getTransaction().isActive())
//                session.getTransaction().rollback();
            e.printStackTrace();
        }
        return "";
    }

    public Hoadon[] getHoadonLH(int id_user) {
        try {
            session.getTransaction().begin();
            String sql = "from Hoadon where id_user= ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, id_user);
            List result = query.list();
            Hoadon[] hoadonList = new Hoadon[result.size()];
            result.toArray(hoadonList);
            session.flush();
            session.getTransaction().commit();
            return hoadonList;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public int countLH(int id) {
        try {
            // session.getTransaction().begin();
            String sql = "from Lienhe where id_user = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, id);
            //Giohang[] result=(Giohang[])query.uniqueResult();
            List<Lienhe> result = query.list();
            int count = result.size();

//            session.flush();
//            session.getTransaction().commit();
            return count;
        } catch (Exception e) {
//            if(session.getTransaction().isActive())
//                session.getTransaction().rollback();
            e.printStackTrace();
        }
        return 0;
    }

    public String phantrang(int sotrang, int get_trang) {
        String html = "";
        //nếu trang hiện tại là trang đầu tiên xuất ra như sau
//                            
        if (get_trang != 1) {
            html += "<a onclick='phantrang(1);'>Start |</a>";
            html += "<a onclick='phantrang(" + (get_trang - 1) + ");'>Back |</a>";
        }
        //bây giờ là bạn xuất các link của các trang.VD 1|2|3.......
        for (int i = 1; i <= sotrang; i++) {
            if (i == get_trang) {
                html += i + "|";
            } else {
                html += "<a onclick='phantrang(" + i + ");'>" + i + " |</a>";
            }
        }
        //bây giờ nếu trang hiện tại là trang cuối xuất ra như sau
//                            
        if (get_trang != sotrang) {

            html += "<a  onclick='phantrang(" + (get_trang + 1) + ");'>Next |</a>";
            html += "<a  onclick='phantrang(" + sotrang + ");'>END |</a>";
        }
        return html;
    }

    //cua nga
    public List<Lienhe> getLienheLikeName(String name) {
        session.beginTransaction().begin();
        String sql = "from Lienhe where ten like ?";
        try {
            Query query = session.createQuery(sql);
            query.setString(0, "%" + name + "%");
            List result = query.list();
            //  Lienhe[] lienheList = new Lienhe[result.size()];
            //   result.toArray(lienheList);
            session.flush();
            // session.getTransaction().commit();
            return result;
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }
        return null;
    }
    // cuaNga

    public List<Lienhe> getLienheByCapbac(Capbac cb) {
        session.beginTransaction().begin();
        String sql = "from Lienhe where capbac =?";
        try {
            Query query = session.createQuery(sql);
            query.setEntity(0, cb);
            List result = query.list();
            // Lienhe[] arrlh = new Lienhe[result.size()];
            //  result.toArray(arrlh);
            session.flush();
            //  session.getTransaction().commit();
            return result;

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
//cua nga

    public List<Lienhe> getLienheByQuanhuyen(Quanhuyen qh) {
        session.getTransaction().begin();
        String sql = "from Lienhe where quanhuyen= ?";
        try {

            Query query = session.createQuery(sql);
            query.setEntity(0, qh);
            List result = query.list();
            //  Lienhe[] arrlh = new Lienhe[result.size()];
            //   result.toArray(arrlh);
            session.flush();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
//cua nga

    public List<Lienhe> getLienheByCongty(Congty ct) {

        session.getTransaction().begin();
        String sql = "from Lienhe where congty= ?";
        try {

            Query query = session.createQuery(sql);
            query.setEntity(0, ct);
            List result = query.list();
            // Lienhe[] arrlh = new Lienhe[result.size()];
            // result.toArray(arrlh);
            session.flush();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
//cua nga

    public List<Lienhe> getLienheByBophan(Bophan bp) {
        session.getTransaction().begin();
        String sql = "from Lienhe where bophan= ?";
        try {

            Query query = session.createQuery(sql);
            query.setEntity(0, bp);
            List result = query.list();
            //  Lienhe[] arrlh = new Lienhe[result.size()];
            //   result.toArray(arrlh);
            session.flush();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    //cua Nga
    public List<Lienhe> getLienheByNganh(Nganh nganh) {
        session.getTransaction().begin();
        String sql = "from Lienhe where nganh= ?";
        try {

            Query query = session.createQuery(sql);
            query.setEntity(0, nganh);
            List result = query.list();
            //  Lienhe[] arrlh = new Lienhe[result.size()];
            //   result.toArray(arrlh);
            session.flush();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
// cua nga

    public List<Lienhe> getLienhebyThanhpho(Thanhpho tp) {
        QuanhuyenDAO quandao = new QuanhuyenDAO();
        List<Quanhuyen> qhlist = quandao.getQuanhuyenByTP(tp);
        List<Lienhe> lhlist = new ArrayList<Lienhe>();
        for (int i = 0; i < qhlist.size(); i++) {
            LienheDAO lienhedao = new LienheDAO();
            List<Lienhe> list2 = lienhedao.getLienheByQuanhuyen(qhlist.get(i));
            for (int j = 0; j < list2.size(); j++) {
                lhlist.add(list2.get(j));
            }
        }
        return lhlist;
    }

    /*  public Lienhe[] getLienHe() {
     try {
     session.getTransaction().begin();
     String sql = "from Lienhe";
     Query query = session.createQuery(sql);
     List result = query.list();
     Lienhe[] lienheList = new Lienhe[result.size()];
     result.toArray(lienheList);
     for (int i = 0; i < lienheList.length; i++) {
     for (int j = i + 1; j < lienheList.length; j++) {
     Date d1 = lienheList[i].getNgay();
     Date d2 = lienheList[i + 1].getNgay();
     if (compareDate(d1, d2)) {
     Lienhe temp = lienheList[i];
     lienheList[i] = lienheList[j];
     lienheList[j] = temp;
     }
     }
     }
     session.flush();
     session.getTransaction().commit();
     return lienheList;
     } catch (Exception e) {
     if (session.getTransaction().isActive()) {
     session.getTransaction().rollback();
     }
     e.printStackTrace();
     }
     return null;
     }

     public boolean compareDate(Date d1, Date d2) {
     if (d1.compareTo(d2) > 0) {
     return true;
     } else {
     return false;
     }
     }
     */
    /*   public static void main(String[] args) throws ParseException {
     LienheDAO A=new LienheDAO();
     String ngaysinh="1293-2-3";
     SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
     Date dt = ft.parse(ngaysinh);
     Lienhe B=new Lienhe("nga",1,dt,5,3,2,2,"21414","aks@gmail.com","abc",1,0);
     A.Save(B);
     }
     */
    /*   public static void main(String[] args) {
 
     DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
     //get current date time with Date()
     Date date = new Date();
     System.out.println(dateFormat.format(date));
 
     //get current date time with Calendar()
     // Calendar cal = Calendar.getInstance();
     // System.out.println(dateFormat.format(cal.getTime()));
 
     }*/
    public Lienhe[] getThangNam(String thang, String nam) {
        try {
            session.getTransaction().begin();
            String sql = "from Lienhe where month(ngay)=? and year(ngay)=?";
            Query query = session.createQuery(sql);
            query.setString(0, thang);
            query.setString(1, nam);
            List result = query.list();
            Lienhe[] lienheList = new Lienhe[result.size()];
            result.toArray(lienheList);
            session.flush();
            session.getTransaction().commit();
            return lienheList;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        LienheDAO a = new LienheDAO();
        ThanhphoDAO tpdao = new ThanhphoDAO();
        Thanhpho tp = tpdao.getThanhphoById(61);
        List<Lienhe> lhlist = a.getLienhebyThanhpho(tp);
        for (int i = 0; i < lhlist.size(); i++) {
            System.out.println(lhlist.get(i).getTen());
        }
    }
}
